﻿namespace cludg.Services.Security
{
	using Domain.Users;

	public static class AdministrationHelper
	{
		public static bool TryRegisterUser(
			this IAdministration administration,
			User user,
			string name,
			string password,
			bool needActivation,
			out string userId)
		{
			RegisterUserError error;
			return TryRegisterUser(administration, user, name, password, needActivation, out userId, out error);
		}

		public static bool TryRegisterUser(
			this IAdministration administration,
			User user,
			string name,
			string password,
			bool needActivation,
			out string userId,
			out RegisterUserError error)
		{
			userId = string.Empty;
			error = null;
			try
			{
				userId = administration.RegisterUser(user, name, password, needActivation);
				return true;
			}
			catch (RegisterUserError e)
			{
				error = e;
				return false;
			}
		}

		public static bool TryRegisterThirdPartyUser(this IAdministration administration, User user, string thirdPartyIdent, out string userId)
		{
			RegisterUserError error;
			return TryRegisterThirdPartyUser(administration, user, thirdPartyIdent, out userId, out error);
		}

		public static bool TryRegisterThirdPartyUser(this IAdministration administration, User user, string thirdPartyIdent,
			out string userId, out RegisterUserError error)
		{
			userId = "";
			error = null;
			try
			{
				userId = administration.RegisterThirdPartyUser(user, thirdPartyIdent);
				return true;
			}
			catch (RegisterUserError e)
			{
				error = e;
				return false;
			}
		}

		public static void ActivateUser(this IAdministration administration, User user)
		{
			administration.ActivateUser(user.Id);
		}
	}
}
